#pragma once 
#include "reactor.hpp"
#include "util.hpp"
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <string>
#include <vector>
#include <unistd.h>

namespace my_interface
{
  using namespace my_epoll;

  int recver(EventItem*);
  int sender(EventItem*);
  int errorer(EventItem*);

  int accepter(EventItem* item)
  {
    std::cout<<" 建立链接:"<<item->sock<<std::endl;

    while(true)
    {
      struct sockaddr_in _peer;
      socklen_t len=sizeof(_peer);
      int sock=accept(item->sock,(struct sockaddr*)&_peer,&len);
      if(sock<0)
      {
        if(errno==EAGAIN || errno==EWOULDBLOCK)
        {
          return 0;
        }else if(errno==EINTR)
        {
          continue;
        }else
        {
          //item->error_handler(item)
          return -1;
        }
      }else{
        my_util::SetNonBlock(sock);
        my_epoll::EventItem tmp;
        tmp.sock=sock;
        tmp.R=item->R;
        tmp.ManagerCallBack(recver,sender,errorer);
        
        item->R->AddEvent(sock,EPOLLIN|EPOLLET,tmp);
      }
    }
  }
  int recv_handler(int sock,std::string* out)
  {
    while(true)
    {
      char buff[1024];
      ssize_t size=recv(sock,buff,sizeof(buff)-1,0);
      if(size<0)
      {
        if(errno==EAGAIN||errno==EWOULDBLOCK)
        {
          return 0;
        }else if(errno==EINTR)
        {
          continue;
        }else
        {
          return -1;
        }
      }else 
      {
        buff[size]=0;
        *out+=buff;
      }
    }
  }
  int recver(EventItem* item)
  {
    sleep(2);
    std::cout<<item->sock<<" 开始读数据 "<<std::endl;
    if(recv_handler(item->sock,&item->inbuffer))
    {
      errorer(item);
      return -1;
    }
    std::cout<<item->inbuffer<<std::endl;

    std::vector<std::string> messages;
    my_util::StringUtil::Split(item->inbuffer, &messages, "X");

    for(auto s:messages)
    {
      std::cout<<s<<std::endl;
    }
    struct data{
            int x;
            int y;
        };
        //已经拿到了所有的数据报文 
        for(auto s : messages)
       {
            
            struct data d;
            my_util::StringUtil::Deserialize(s, &d.x, &d.y);
                  //4. 业务处理
           int z = d.x + d.y ;                       
        std::string response;
        response += std::to_string(d.x);
         response += "+";          
       response += std::to_string(d.y);
       response += "=";
       response += std::to_string(z); 
       item->outbuffer += response;
       //5.1 设置响应报文和响应报文之间的分                    
   item->outbuffer += "X"; 
   std::cout<<item->outbuffer<<std::endl;
      }
  //6. 写回
  if(!item->outbuffer.empty()) item->R->EnableReadWrite(item->sock, true, true);
  std::cout<<"555555"<<std::endl;
    return 0;
  }
 
  int sender_helper(int sock,std::string &in)
  {
		size_t total = 0;
    while(true)
    {
       ssize_t s = send(sock, in.c_str()+total, in.size()-total, 0);
       if(s > 0)
        {
        total += s;
           if(total >= in.size())
            {
             return 0;
             }
        } else if(s < 0)
        {
          if(errno == EAGAIN || errno == EWOULDBLOCK)
           {
            in.erase(total);
						return 1;
           }else if(errno==EINTR)
            {
              continue;
            }else
							{
							return -1;
             }
        }


    }
  }
  int sender(EventItem* item)
  {
    std::cout<<"wr:"<<item->outbuffer<<std::endl;
   int ret = sender_helper(item->sock, item->outbuffer);

    std::cout<<"wr:完"<<item->outbuffer<<std::endl;
    if(ret == 0)
    {
      std::cout<<"写事件关"<<std::endl;
     item->R->EnableReadWrite(item->sock, true, false);
    }
    else if(ret == 1)
    {
      std::cout<<"写事件关"<<std::endl;
      item->R->EnableReadWrite(item->sock, true, true);
    }else
    {
        errorer(item);
    }
    return 0;
  }
  int errorer(EventItem* item)
  {
    std::cerr<<item->sock<<" error"<<std::endl;
    return 0;
  }
}

